package com.tjrac.generalpermissions.web;

import com.tjrac.filter.entity.impl.ObjectParam;
import com.tjrac.filter.factory.FilterFactories;
import com.tjrac.generalpermissions.domain.Role;
import com.tjrac.generalpermissions.domain.User;
import com.tjrac.generalpermissions.epception.MsgException;
import com.tjrac.generalpermissions.epception.statusCode.LoginCode;
import com.tjrac.generalpermissions.epception.statusCode.SessionPermission;
import com.tjrac.generalpermissions.epception.statusCode.VerificationCode;
import com.tjrac.generalpermissions.service.UserService;
import com.tjrac.generalpermissions.utils.JsonPack;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 用户操作
 *
 * @author ZeNing
 * @create 2020-11-29 17:09
 */
@RestController
public class UserServlet {

    @Resource
    UserService userService;

    /**
     * 登录
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST, produces = "text/html;charset=utf-8")
    String login(
            String username,
            String password,
            HttpSession session
    ) {
        try {
            // TODO 登录后存入权限对象
            User user = userService.login(username, password);
            Role role = user.getRole();
            //存入当前登录用户信息
            session.setAttribute("user", user);
            session.setAttribute(FilterFactories.FILTER_SESSION_ROLE, role);
            if (user.getUsername().equals("root")){
                // ROOT用户可以查询所有的用户
                session.setAttribute(SessionPermission.QUERY_SINGLEUSER_RANGE, userService.queryAllListParam());
            } else {
                // 其他用户只能查询自己
                session.setAttribute(SessionPermission.QUERY_SINGLEUSER_RANGE, new ObjectParam(user.getUsername()));
            }


            return (new MsgException(LoginCode.SUCCESS_USER, "登录成功")).toJson();
        } catch (MsgException e) {
            return e.toJson();
        }
    }

    /**
     * 返回当前用户信息
     */
    @RequestMapping(value = "/getCurrentUserInfo", method = RequestMethod.POST, produces = "text/html;charset=utf-8")
    public String getCurrentUserInfo(
            HttpSession session
    ) {

        try {
            return userService.getCurrentUserInfo(session);
        } catch (MsgException e) {
            return e.toJson();
        } catch (Exception e) {
            e.printStackTrace();
            return new MsgException(VerificationCode.UNKNOWN_ERROR, "未知错误").toJson();
        }

    }

    /**
     * 注销当前用户
     */
    @RequestMapping(value = "/logout", method = RequestMethod.POST, produces = "text/html;charset=utf-8")
    public String logout(
            HttpSession session
    ) {
        return userService.logout(session);
    }

    /**
     * 查询用户信息，如果是root用户返回所有用户信息，否则只返回当前用户信息
     */
    @RequestMapping(value = "/queryUser", method = RequestMethod.POST, produces = "text/html;charset=utf-8")
    public String queryUser(
            HttpSession session
    ) {
        try {
            return userService.queryUser(session);
        } catch (MsgException e) {
            return e.toJson();
        } catch (Exception e) {
            e.printStackTrace();
            return new MsgException(VerificationCode.UNKNOWN_ERROR, "未知错误").toJson();
        }

    }

}
